package com.eselect.controller;

import com.eselect.config.RabbitMQConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/mq")
@Slf4j
public class RabbitMQController {

    private final RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public void send() {
        rabbitTemplate.convertAndSend(RabbitMQConfiguration.SIMPLE_EXCHANGE_DEFAULT, "default", "死信消息，延迟5秒后处理",
                message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setExpiration("5000");    // 单位毫秒
            return message;
        });
        log.info("发送成功");
    }
}
